<?php

namespace App\Http\Controllers\Alipay;
use App\Models\MemberOther;
use App\Services\AlipayBaseService;
use App\Services\AlipayMiniAppService;
use App\Services\AlipayService;
use App\Services\CommonService;
use Illuminate\Http\Request;

class AuthorizationController extends BaseVerifyController
{
    protected $alipayUid;

    const TYPE_SLIENCE_AUTH = 1; //静默授权
    const TYPE_GRANT_AUTH = 2; // 主动授权
    /**
     * 用户授权
     */
    public function grantAuth(Request $request){
        $authCode = $request->input('authCode');
//        $header = $request->header();
        $type = $request->input('type',self::TYPE_SLIENCE_AUTH);
        if(!$authCode || !$type){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }

        $miniId = AlipayMiniAppService::getInstance()->getMiniId();

        $accessTokenData = AlipayService::authToken($miniId, $authCode, 'app');
        if($accessTokenData['status']){
            if($type == self::TYPE_SLIENCE_AUTH){
                $this->alipayUid = $accessTokenData['data']['user_id'];
            }else{
                $info = AlipayService::userInfo($miniId, $accessTokenData['data']['access_token'],'app');

                if($info['status']){
                    $this->alipayUid  = $info['data']['user_id'];
                    //添加更新数据
                    $memberData['head_image'] = isset($info['data']['avatar']) ? $info['data']['avatar'] : '';
                    $memberData['province'] = isset($info['data']['province']) ? $info['data']['province'] : '';
                    $memberData['city'] = isset($info['data']['city']) ? $info['data']['city'] : '';
                    $memberData['nick_name'] = isset($info['data']['nick_name']) ? $info['data']['nick_name'] : '';
                    $gender = isset($info['data']['gender']) ? $info['data']['gender'] : '';
                    $memberData['gender'] =AlipayBaseService::gender($gender); ;
                    //返回数据
                    $data['avatar'] = $memberData['head_image'];
                    $data['nickName'] = $memberData['nick_name'];
                }
            }
            $memberData['user_id'] = $this->alipayUid;
            $memberData['token'] = createToken($type.$this->alipayUid);
            $memberData['update_time'] = time();

            if(MemberOther::where(['user_id'=>$this->alipayUid])->first()){
                MemberOther::where(['user_id'=>$this->alipayUid])->update($memberData);
            }else{
                if(empty($memberData['user_id'])){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'授权失败');
                }
                $memberData['sale_user_id'] = $request->input('sale_user_id')??'';
                $memberData['equipment'] = $request->input('equipment')??'';
                $memberData['create_time'] = time();
                MemberOther::where(['user_id'=>$this->alipayUid])->insert($memberData);
            }
            $data['alipayUid'] = $this->alipayUid;
            $data['token'] = $memberData['token'];
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'授权成功',$data);
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'授权失败');
    }


}
